package com.example.androidexample.animation;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.IdRes;

import com.example.androidexample.R;

public class ScaleAnimationBuilder implements AnimationBuilder, SeekBar.OnSeekBarChangeListener, View.OnClickListener {

    private final AnimationActivity activity;
    private final AnimationPivotParamsProvider pivotParamsBuilder;
    private float fromX = 1f;
    private float toX = 2f;
    private float fromY = 1f;
    private float toY = 2f;
    private TextView tvScaleFromX;
    private TextView tvScaleFromY;
    private TextView tvScaleToX;
    private TextView tvScaleToY;


    public ScaleAnimationBuilder(AnimationActivity activity, AnimationPivotParamsProvider pivotParamsBuilder) {
        this.activity = activity;
        this.pivotParamsBuilder = pivotParamsBuilder;
        resetXYValue();
        initView();
    }

    private void initView() {
        SeekBar skbScaleFromX = activity.findViewById(R.id.skb_scale_from_x);
        skbScaleFromX.setOnSeekBarChangeListener(this);
        tvScaleFromX = activity.findViewById(R.id.tv_scale_from_x);

        SeekBar skbScaleFromY = activity.findViewById(R.id.skb_scale_from_y);
        skbScaleFromY.setOnSeekBarChangeListener(this);
        tvScaleFromY = activity.findViewById(R.id.tv_scale_from_y);

        SeekBar skbScaleToX = activity.findViewById(R.id.skb_scale_to_x);
        skbScaleToX.setOnSeekBarChangeListener(this);
        tvScaleToX = activity.findViewById(R.id.tv_scale_to_x);

        SeekBar skbScaleToY = activity.findViewById(R.id.skb_scale_to_y);
        skbScaleToY.setOnSeekBarChangeListener(this);
        tvScaleToY = activity.findViewById(R.id.tv_scale_to_y);

        Button btScale = activity.findViewById(R.id.bt_scale);
        btScale.setOnClickListener(this);
    }

    public void resetXYValue() {
        fromX = 1f;
        toX = 2f;
        fromY = 1f;
        toY = 2f;
    }

    @Override
    public Animation buildAnimation() {
        return new ScaleAnimation(fromX,toX,fromY,toY,pivotParamsBuilder.getPivotXType(),pivotParamsBuilder.getPivotXValue()
                ,pivotParamsBuilder.getPivotYType(),pivotParamsBuilder.getPivotYValue());
    }

    @SuppressLint({"NonConstantResourceId", "SetTextI18n"})
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        @IdRes
        int seekBarId = seekBar.getId();
        switch (seekBarId) {
            case R.id.skb_scale_from_x: {
                fromX = progress/100f;
                tvScaleFromX.setText(fromX+"");
            } break;
            case R.id.skb_scale_from_y: {
                fromY = progress/100f;
                tvScaleFromY.setText(fromY+"");
            } break;
            case R.id.skb_scale_to_x: {
                toX = progress/100f;
                tvScaleToX.setText(toX+"");
            } break;
            case R.id.skb_scale_to_y: {
                toY = progress/100f;
                tvScaleToY.setText(toY+"");
            } break;
        }
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) { }

    @Override
    public void onClick(View v) {
        activity.handleAction(AnimationBuilderType.SCALE,this);
    }
}
